package com.evaluationSystem.controller;

import com.evaluationSystem.config.R;
import com.evaluationSystem.entity.Dimension;
import com.evaluationSystem.entity.Questionnaire;
import com.evaluationSystem.entity.User;
import com.evaluationSystem.entity.vo.QuestionnaireVo;
import com.evaluationSystem.entity.vo.UserVo;
import com.evaluationSystem.service.DimensionService;
import com.evaluationSystem.util.CommonUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 问卷选项控制器
 **/
@RestController
@RequestMapping("/dimension")
@RequiredArgsConstructor
public class DimensionController {
    @Value("${file.titlePath}")
    private String filePath;

    private final DimensionService dimensionService;


    // todo 查找全部
    @RequestMapping(value = "/getDimension", method = RequestMethod.GET)
    public R<List<Dimension>> getDimension(String questionId) {
        return R.success(dimensionService.getDimension(questionId));
    }

    @RequestMapping(value = "/updateDimension", method = RequestMethod.POST)
    public R<String> updateDimension(@RequestBody Dimension dimension) {
        dimensionService.updateDimension(dimension);
        return R.success("更新成功");
    }

    @RequestMapping(value = "/updateDimensionSort", method = RequestMethod.PUT)
    public R<String> updateDimensionSort(@RequestBody List<Dimension> dimension) {
        dimensionService.updateDimensionSort(dimension);
        return R.success("更新成功");
    }

    @RequestMapping(value = "/getQuestion", method = RequestMethod.GET)
    public R<List<Questionnaire>> getQuestion(HttpServletRequest request) {
        UserVo userVo = (UserVo) request.getSession().getAttribute("user");
        return R.success(dimensionService.getQuestion(userVo));
    }

    @RequestMapping(value = "/getQuestionList", method = RequestMethod.GET)
    public R<List<QuestionnaireVo>> getQuestionList(User user) {
        return R.success(dimensionService.getQuestionList(user));
    }


    // todo  新增题目
    @RequestMapping(value = "/insertDimension", method = RequestMethod.POST)
    public R<String> insertDimension(@RequestBody Dimension dimension) {
        dimensionService.insertDimension(dimension);
        return R.success("添加成功");
    }

    // 新增评分
    @RequestMapping(value = "/insertDimensions", method = RequestMethod.POST)
    public R<String> score(@RequestBody Dimension dimension) {
        dimensionService.insertDimensions(dimension);
        return R.success("添加成功");
    }

    @RequestMapping(value = "/insertBathDimension", method = RequestMethod.POST)
    public R<String> insertBathDimension(@RequestBody List<Dimension> dimensionList) {
        dimensionService.insertBathDimension(dimensionList);
        return R.success("添加成功");
    }

    @RequestMapping(value = "/downloadTemplate", method = RequestMethod.GET)
    public void download(HttpServletResponse response, String fileName) {
        CommonUtil.downTemplates(response, fileName, filePath);
    }

    @RequestMapping(value = "/uploadDimension", method = RequestMethod.POST)
    public R<HashMap<String, Object>> uploadDimension(@RequestPart(value = "file") MultipartFile multipartFile, String questionId) {
        HashMap<String, Object> resultMap = dimensionService.uploadDimension(multipartFile, questionId);
        return R.success(resultMap);
    }

    @RequestMapping(value = "/deleteDimension", method = RequestMethod.POST)
    public R<String> deleteDimension(@RequestBody String id) {
        if (id != null && id.endsWith("=")) {
            id = id.substring(0, id.length() - 1);
        }
        dimensionService.deleteDimension(id);
        return R.success("删除成功");
    }
}
